package com.yxq.mdt.app.modules.socialcontact.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.base.ibase.IBaseContract;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalStateBean;
import com.yxq.mdt.app.modules.socialcontact.adapter.SocialStateGiveLikeAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.SocialStateGiveListBrowseAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.SocialStateGiveListLikeAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.BrowseListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.GroupTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveLikeBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialStateGiveListLikeBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPageInfoContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialPgaeInfoPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.浏览
 */
public class SocialStateBrowseFragment extends BaseFragment<MainSocialPgaeInfoPresenter> implements MainSocialPageInfoContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_like_recycler)
    RecyclerView socialLikeRecycler;
    SocialStateGiveListBrowseAdapter socialStateGiveLikeAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<BrowseListBean.ListBean> socialStateGiveLikeBeans;
    private String topicId = "", give = "", id = "";

    @Override
    protected int attachLayoutId() {
        setImmersionBarEnabled(false);
        return R.layout.fragment_social_state_browse;
    }

    @Override
    protected MainSocialPgaeInfoPresenter getPresenter() {
        return new MainSocialPgaeInfoPresenter(this, new MainSocialContactModel());
    }


    @Override
    public void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            topicId = bundle.getString("topicId");
            give = bundle.getString("give");
            id = bundle.getString("id");
        }
        socialStateGiveLikeBeans = new ArrayList<>();
        socialLikeRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        socialStateGiveLikeAdapter = new SocialStateGiveListBrowseAdapter(R.layout.item_social_like_list, socialStateGiveLikeBeans);
        socialLikeRecycler.setAdapter(socialStateGiveLikeAdapter);
        socialStateGiveLikeAdapter.setEmptyView(R.layout.layout_empty_view, socialLikeRecycler);
        socialStateGiveLikeAdapter.isUseEmpty(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getBrowseListList(id, String.valueOf(20), String.valueOf(page));
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getBrowseListList(id, String.valueOf(20), String.valueOf(page));
        });
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onLoadPageInfoSuccess(MainPersonalStateBean mainPersonalStateBean) {

    }

    @Override
    public void onLoadPageInfoFailed() {

    }

    @Override
    public void onLoadTopicByIdSuccess(GroupTopicListBean groupTopicListBean) {

    }

    @Override
    public void onLoadTopicByIdFailed() {

    }

    @Override
    public void onLoadGiveFailed() {

    }

    @Override
    public void onLoadGiveSuccess(SocialStateGiveListLikeBean socialStateGiveListLikeBean) {

    }

    @Override
    public void onLoadBrowseListFailed() {

    }

    @Override
    public void onLoadBrowseListSuccess(BrowseListBean browseListBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            socialStateGiveLikeAdapter.setNewData(browseListBean.getList());
        } else {
            if (browseListBean.getList() != null && browseListBean.getList().size() != 0) {
                socialStateGiveLikeAdapter.addData(browseListBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }

        socialStateGiveLikeAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
            it.putExtra("userId", socialStateGiveLikeAdapter.getItem(position).getCreateMan() + "");
            startActivity(it);
        });
        socialStateGiveLikeAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.social_attention_tv:
                    MainSocialContactModel.setInterestRelation(socialStateGiveLikeAdapter.getItem(position).getCreateMan() + "", 0, 1, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            showCenterToast("关注成功");
                            refreshLayout.autoRefresh();
                        }

                        @Override
                        public void onError(String errMsg) {
                            showCenterToast("关注失败");
                        }
                    });
                    break;
                default:
            }
        });
        socialStateGiveLikeAdapter.isUseEmpty(!(socialStateGiveLikeAdapter.getData().size() > 0));
    }


    @Override
    public void onLoadTopicFavorSuccess(String s) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicFavorFailed() {

    }
}
